package com.swyc.osschat.Test.designMode.agent.run;

import com.swyc.osschat.Test.designMode.agent.base.RentHouse;
import com.swyc.osschat.Test.designMode.agent.impl.Homeowner;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @description 代理工厂类(通过此类获取代理对象)：
 * @Author: z156940l@outlook.com
 * @className ProxyFactory
 * @Date: 2022/7/25 17:56
 */
public class ProxyFactory {
    //声明房东类
    private Homeowner homeowner = new Homeowner();

    public RentHouse getProxyObj(){
        //获取代理对象
        RentHouse rentHouse = (RentHouse) Proxy.newProxyInstance(
                homeowner.getClass().getClassLoader(),
                homeowner.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("中介收了点中介费");
                        Object result = method.invoke(homeowner,args);
                        return result;
                    }
                }
        );
        return rentHouse;
    }
}
